package com.li.recursion;

public class MLGong {
    public static void main(String[] args) {
        int[][] map = new int[8][7];
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        //设置挡板
        map[3][1] = 1;
        map[3][2] = 1;

        //使用递归回溯找路
        setWay(map, 1, 1);


        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + "");
            }
            System.out.println();

        }
    }

    public static boolean setWay(int[][] map, int i, int j) {
        int s = 0;
        if (map[6][5] == 2) {//通路走通
            return true;
        } else {
            if (map[i][j] == 0) {//这个点没走过
                map[i][j] = 2;//可以走通
                if (setWay(map, i + 1, j)) {//下
                    s++;
                    return true;
                } else if (setWay(map, i, j + 1)) {//右走
                    s++;
                    return true;
                } else if (setWay(map, i - 1, j)) {//上走
                    s++;
                    return true;
                } else if (setWay(map, i, j - 1)) {//左
                    s++;
                    return true;
                } else {
                    map[i][j] = 3;//走过，但是走不通
                    return false;
                }
            } else {

                return false;
            }

        }

    }
}
